package com.wpf.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wpf.system.common.utils.MakeTree;
import com.wpf.system.entity.Field;
import com.wpf.system.entity.UserRole;
import com.wpf.system.entity.vo.FieldVO;
import com.wpf.system.service.FieldService;
import com.wpf.system.mapper.FieldMapper;
import com.wpf.system.service.UserRoleService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author ferna
* @description 针对表【field(场馆表)】的数据库操作Service实现
* @createDate 2023-01-13 13:23:42
*/
@Service
public class FieldServiceImpl extends ServiceImpl<FieldMapper, Field> implements FieldService{
    @Resource
    private UserRoleService userRoleService;

    @Value("${system.rootDefaultRole}")
    private Long rootDefaultRole;

    @Override
    public List<Field> getFieldList(Long uid) {
        //查询用户是否具有管理员角色身份
        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.lambda().eq(UserRole::getUid,uid);
        userRoleQueryWrapper.lambda().eq(UserRole::getRid,rootDefaultRole);
        UserRole userRole = userRoleService.getBaseMapper().selectOne(userRoleQueryWrapper);
        LambdaQueryWrapper<Field> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Field::getSort);
        if(ObjectUtil.isNull(userRole)){
            //如果不是管理员，返回自身负责的场馆信息
            queryWrapper.eq(Field::getUid,uid);
        }
        //如果是，返回全部场馆
        List<Field> fieldList = this.baseMapper.selectList(queryWrapper);
        return MakeTree.makeFieldTree(fieldList,0L);
    }

    @Override
    public Boolean updateWeekBatchById(String week,Long id) {
        ArrayList<Long> ids = new ArrayList<>();
        ids.add(id);
        this.getIds(ids,id);
        //批量修改开放星期
        return baseMapper.updateWeekBatchById(week,ids);
    }

    @Override
    public Boolean editFieldUser(Long uid, Long id) {
        ArrayList<Long> ids = new ArrayList<>();
        ids.add(id);
        this.getIds(ids,id);
        //批量修改负责人
        return baseMapper.editFieldUser(uid,ids);
    }

    @Override
    public List<FieldVO> getValidField(Long pid) {
        return this.baseMapper.getValidField(pid);
    }

    public void getIds(ArrayList<Long> ids, Long oneId) {
        //查询二级分类的对象
        QueryWrapper<Field> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Field::getPid,oneId);
        List<Field> childrenList = baseMapper.selectList(queryWrapper);
        //遍历二级分类的对象，把二级分类的id加入到要删除的集合中
        for (Field children : childrenList) {
            Long id = children.getId();
            ids.add(id);
            //把二级分类的每一个ID，查询它下面的子节点
            this.getIds(ids,id);
        }
    }
}




